package com.zsule.frameworks.mongodb.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import com.zsule.entity.MongoEntity;

@Repository
public class MongoDao <T extends MongoEntity>{
	
	@Resource
	private MongoTemplate mongoTemplate;

	public T findById(String id, Class<T> clazz) {
	    return mongoTemplate.findById(id, clazz);
	}
	
	public T findOne(Query query, Class<T> clazz) {
		return mongoTemplate.findOne(query, clazz);
	}
	
	public List<T> findList(Query query, Class<T> clazz) {
		return mongoTemplate.find(query, clazz);
	}
	
    public List<T> findAll(Class<T> clazz) {
        return mongoTemplate.findAll(clazz);
    }

	public void remove(T entity) {
		mongoTemplate.remove(entity);
	}
	
	public void remove(Query query, Class<T> clazz) {
        mongoTemplate.remove(query, clazz);
    }
	
	public T findAndRemoveFirst(Query query, Class<T> clazz) {
        return mongoTemplate.findAndRemove(query, clazz);
    }
	
	public List<T> findAndRemoveAll(Query query, Class<T> clazz) {
        return mongoTemplate.findAllAndRemove(query, clazz);
    }
	
	public long size(Query query, Class<T> clazz) {
		return mongoTemplate.count(query, clazz);
	}

	public void save(T entity) {
		mongoTemplate.save(entity);
	}

	public void insert(T entity) {
		mongoTemplate.insert(entity);
	}

	public void insertBatch(List<T> entitys, Class<T> clazz) {
		mongoTemplate.insert(entitys, clazz);
	}

	public void updateAll(Query query, Update update, Class<T> clazz) {
		mongoTemplate.updateMulti(query, update, clazz);
	}
	
	public void updateFirst(Query query, Update update, Class<T> clazz) {
        mongoTemplate.updateFirst(query, update, clazz);
    }
	
	public T findAndModify(Query query, Update update, Class<T> clazz) {
        return mongoTemplate.findAndModify(query, update, clazz);
    }

	public MongoTemplate getMongoTemplate() {
        return mongoTemplate;
    }

    public void setMongoTemplate(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

}
